//
//  TTDepaturesViewController.m
//  TrainTrack
//
//  Created by Jonathan Bunde-Pedersen on 04/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TTDepaturesViewController.h"
#import "TrainTrackAppDelegate.h"
#import "TTDepartureCell.h"

@implementation TTDepaturesViewController

@synthesize station, departures;

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
	    
			    //self.title = NSLocalizedString(@"All Station", @"AllStations");
	    
	    CGRect application_frame = [[UIScreen mainScreen] applicationFrame];
	    application_frame = CGRectMake(application_frame.origin.x, application_frame.origin.y - 20, application_frame.size.width, application_frame.size.height-45);
	    
	    self.view.frame = application_frame;
	    
		self.tableView = [[UITableView alloc] initWithFrame:application_frame style:UITableViewStylePlain];
		self.tableView.delegate = self;
		self.tableView.dataSource = self;
		self.tableView.rowHeight = 56;
		self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
		self.tableView.sectionHeaderHeight = 0;
		self.tableView.multipleTouchEnabled = YES;
		self.tableView.delaysContentTouches = YES;
		self.tableView.scrollEnabled = YES;
	    
	    UIBarButtonItem *starBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(starClicked)];
	    [self.navigationItem setRightBarButtonItem:starBarButton];
    }
    return self;
}

- (void) starClicked {
	TrainTrackAppDelegate *appdelegate = (TrainTrackAppDelegate *)[[UIApplication sharedApplication] delegate];
	[appdelegate addFavStation:self.station];
}

/*
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSLog(@"%@", [self.departures description]);
    return [self.departures count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"TTDepartureCell";
    
    TTDepartureCell *cell = (TTDepartureCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TTDepartureCell alloc] initWithFrame:CGRectMake(0, 0, 320, self.tableView.rowHeight-1) reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell
	cell.departure = [self.departures objectAtIndex:indexPath.row];
	//cell.text = @"test";
	return cell;
}

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
*/

/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    }
    if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}
*/

/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	TrainTrackAppDelegate *appController = (TrainTrackAppDelegate *)[[UIApplication sharedApplication] delegate];

	self.title = self.station;
	self.departures = [appController.model fetchValidDeparturesForStation:self.station];
	[self.tableView reloadData];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/
/*
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
*/

- (void)dealloc {
    [super dealloc];
}


@end

